package be.aquafin.klipdf.service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.codehaus.jettison.json.JSONArray;
import org.springframework.stereotype.Component;

import be.aquafin.klipdf.dao.DataDao;
import be.aquafin.klipdf.handlers.DataHandler;
import be.aquafin.klipdf.handlers.NodeHandler;
import be.aquafin.klipdf.handlers.PipeHandler;
import be.aquafin.klipdf.model.Objecttype;
import be.aquafin.klipdf.model.SpatialObject;
import be.aquafin.klipdf.model.json.JSONObject;

/**
 * @author user2080
 * 
 *         JSON-implementatie DataService
 * 
 */
public class DataServiceImpl implements DataService {
	private final Map<Objecttype, DataHandler> dataHandlers = new ConcurrentHashMap<Objecttype, DataHandler>();

	@Override
	public String getData(Objecttype objecttype, List<? extends SpatialObject> spatialObjects) {
		if (!dataHandlers.containsKey(objecttype)) {
			throw new IllegalArgumentException("DataHandler not found for [" + objecttype + "]");
		}
		DataHandler dataHandler = dataHandlers.get(objecttype);
		JSONArray jsonArray = dataHandler.process(spatialObjects);
		JSONObject result = new JSONObject();
		result.put(objecttype.toString(), jsonArray);
		return result.toString();
	}

	public void setDataDao(DataDao dataDao) {
		dataHandlers.put(Objecttype.KNOOP, new NodeHandler(dataDao));
		dataHandlers.put(Objecttype.LEIDING, new PipeHandler(dataDao));
	}

}